package com.xiang.plane.model;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.xiang.plane.util.FrameRate;
import com.xiang.plane.util.ImageUtil;
import com.xiang.plane.view.GameView;

/**
 * Created by xiangliyou on 2017/8/9.
 * 背景
 */

public class Background {
    //背景图片
    private Bitmap backBmp;
    //绘画图片的y坐标,使用一张图片滚动
    private float y1, y2;
    //背景移动的速度
    private int moveSpeed = 3;
    //显示的fps
    private FrameRate fps;

    public Background() {
        this.backBmp = ImageUtil.ImageCacahe.get(ImageUtil.BACK_1_KEY);
        y1 = -Math.abs(backBmp.getHeight() - GameView.screenH);
        y2 = y1 - backBmp.getHeight();

        fps = new FrameRate();
        fps.initialize();
    }

    /**
     * 绘画背景
     * @param canvas
     * @param paint
     */
    public void draw(Canvas canvas, Paint paint) {
        //将一张图片绘制两次
        canvas.drawBitmap(backBmp, 0, y1, paint);
        canvas.drawBitmap(backBmp, 0, y2, paint);
        paint.setStrokeWidth(1f);
        //绘制fps文字
        paint.setColor(Color.RED);
        paint.setTextSize(28);
        canvas.drawText(fps.getFrameRate(), 10, 30, paint);
        canvas.drawText("Score:"+GameView.Score, 120, 30, paint);
    }

    /**
     * 背景逻辑，移动背景
     */
    public void logic() {
        y1 += moveSpeed;
        y2 += moveSpeed;
        //当第一张图片的y坐标超出屏幕，将其放在第二张上面
        if (y1 > GameView.screenH) {
            y1 = y2 - backBmp.getHeight();
        }
        //当第二张图片的y坐标超出屏幕，将其放在第一张上面
        if (y2 > GameView.screenH) {
            y2 = y1 - backBmp.getHeight();
        }

        //更新fps
        fps.calculate();
    }

}
